<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'core.upload.class.php');
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'core.file.class.php');
include_once(CONFIG_PATH . CONFIG_DIR_FUNCTIONS . 'thumb.function.php');
include_once(CONFIG_PATH . CONFIG_DIR_FUNCTIONS . 'attachment.function.php');

class CoreUploadThumb {
	var $_inited = false;
	
	//
	var $save_path = '';
	var $thumb_path = '';
	var $resize_path = '';
	
	//
	//name
	//0		no
	//1		-%s
	var $name_type = 0;
	var $thumb_name = '-thumb';
	var $resize_name = '-resize';
	
	//thumb
	//0		no
	//1		generate
	var $generate_thumb = 1;
	var $generate_resize = 1;
	
	//constructor
	function CoreUploadThumb() {
		
	}
	
	function _init() {
		if( $this->_inited == true ) {
			return false;
		}
		$this->save_path = CONFIG_PATH . CONFIG_DIR_UPLOADS_ORIGINS;
		if( $this->name_type == 1 ) {
			$this->thumb_path = CONFIG_PATH . CONFIG_DIR_UPLOADS_THUMBS;
			$this->resize_path = CONFIG_PATH . CONFIG_DIR_UPLOADS_RESIZED;
		}
		if( $this->thumb_path == '' ) {
			$this->name_type = 1;
			$this->thumb_path = $this->save_path;
		}
		if( $this->resize_path == '' ) {
			$this->name_type = 1;
			$this->resize_path = $this->save_path;
		}
		$this->_inited = true;
	}
	
	//methods
	function upload() {
		$this->_init();
		$this->up_image_filename = '';
		$this->up_image_save_file = '';
		
		//
		$up_image_obj = new CoreUpload('field_imageupload');
		$up_image_obj->setDestination($this->save_path);
		if( $up_image_obj->parse() && $up_image_obj->save() ) {
			$up_image_filename = $up_image_obj->save_filename;
			$up_image_save_file = $up_image_obj->save_file;
			
			//thumbs
			if( $this->generate_thumb == 1 ) {
				$thumb_file = $this->thumb_path . $this->_thumb_name($up_image_filename);
				$thumb_width = CONST_IMAGE_THUMB_WIDTH;
				$thumb_height = CONST_IMAGE_THUMB_HEIGHT;
				func_thumb_generate_thumb($up_image_save_file, $thumb_file, $thumb_width, $thumb_height);
			}
			//resized
			if( $this->generate_resize == 1 ) {
				$thumb_file = CONFIG_PATH . CONFIG_DIR_UPLOADS_RESIZED . $this->_resize_name($up_image_filename);
				$thumb_width = CONST_IMAGE_RESIZE_WIDTH;
				$thumb_height = CONST_IMAGE_RESIZE_HEIGHT;
				func_thumb_generate_thumb($up_image_save_file, $thumb_file, $thumb_width, $thumb_height);
			}
		}
		//
		$this->up_image_filename = $up_image_filename;
		$this->up_image_save_file = $up_image_save_file;
	}
	
	//
	function uploads() {
		$up_images_data = array();
		$up_images_ids = array();
		$up_images_obj = new CoreMultiUpload('field_imagesupload');
		$up_images_obj->setDestination($this->save_path);
		if( $up_images_obj->parse() && $up_images_obj->save() ) {
			$up_images_filename = $up_images_obj->filename;
			$up_images_filetype = $up_images_obj->filetype;
			$up_images_filesize = $up_images_obj->file['size'];
			$up_images_file = $up_images_obj->save_filename;
			$up_images_save_file = $up_images_obj->save_file;
			
			for($i=0, $n=sizeof($up_images_save_file); $i<$n; $i++) {
				$up_fileObj = new CoreFile($up_images_save_file[$i]);
				$up_fileObj->parse();
				
				$up_images_extension = $up_fileObj->extension;
				$up_images_attribute = $up_fileObj->getFileAttribute();
				$up_images_width = $up_fileObj->getImageWidth();
				$up_images_height = $up_fileObj->getImageHeight();
				
				//thumbs
				if( $this->generate_thumb == 1 ) {
					$thumb_file = $this->thumb_path . $this->_thumb_name($up_images_file[$i]);
					$thumb_width = CONST_IMAGE_THUMB_WIDTH;
					$thumb_height = CONST_IMAGE_THUMB_HEIGHT;
					func_thumb_generate_thumb($up_images_save_file[$i], $thumb_file, $thumb_width, $thumb_height);
				}
				//resized
				if( $this->generate_resize == 1 ) {
					$thumb_file = $this->resize_path . $this->_resize_path($up_images_file[$i]);
					$thumb_width = CONST_IMAGE_RESIZE_WIDTH;
					$thumb_height = CONST_IMAGE_RESIZE_HEIGHT;
					func_thumb_generate_thumb($up_images_save_file[$i], $thumb_file, $thumb_width, $thumb_height);
				}
				
				//
				$up_images_data[] = array(
							'images_name' => $up_images_filename[$i],
							'images_description' => $_POST['field_imagesdesc'][$i],
							'images_value' => $up_images_file[$i],
							'images_filename' => $up_images_filename[$i],
							'images_filetype' => $up_images_filetype[$i],
							'images_filesize' => $up_images_filesize[$i],
							'images_width' => $up_images_width,
							'images_height' => $up_images_height,
							'images_extension' => $up_images_extension,
								);
				
			}
		}
		//
		$this->up_images_data = $up_images_data;
	}
	
	//
	function _thumb_name($filename) {
		if( $this->name_type == 1 ) {
			if( ($pos = strrpos($filename, '.')) !== false ) {
				$filename = substr($filename, 0, $pos) . $this->thumb_name . substr($filename, $pos);
			} else {
				$filename .= $this->resize_name;
			}
		}
		return $filename;
	}
	
	function _resize_name($filename) {
		if( $this->name_type == 1 ) {
			if( ($pos = strrpos($filename, '.')) !== false ) {
				$filename = substr($filename, 0, $pos) . $this->resize_name . substr($filename, $pos);
			} else {
				$filename .= $this->resize_name;
			}
		}
		return $filename;
	}
}
//
?>